package com.shareach.dao.mongo.impl;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.shareach.common.BaseQueryOrder;
import com.shareach.common.CollectionNames;
import com.shareach.dao.MongoDBBasicDao;
import com.shareach.dao.mongo.BankCollectDao;
import com.shareach.domain.BankCollectDomain;

@Component
public class BankCollectDaoImpl extends MongoDBBasicDao<BankCollectDomain> implements BankCollectDao {

	@Override
	public void insert(BankCollectDomain domain) {
		domain.setTime(new Date());
		super.save(domain);
	}

	@Override
	public <T> void delete(Query query, Class<T> clas) {
		mongoTemplate.remove(query, clas);
	}

	@Override
	public void update(Criteria criteria, Update update) {
		super.update(criteria, update, BankCollectDomain.class);
	}

	@Override
	public List<BankCollectDomain> findList(Criteria criteria) {
		return super.findList(criteria, BankCollectDomain.class);
	}

	@Override
	public List<BankCollectDomain> findList(Query query) {
		return super.findList(query, BankCollectDomain.class);
	}

	@Override
	public BankCollectDomain findOne(Query query) {
		return super.findOne(query, BankCollectDomain.class);
	}

	@Override
	public <T> MapReduceResults<T> groupMapReduce(Query query, String map, String reduce, Class<T> clas) {
		return mongoTemplate.mapReduce(query, CollectionNames.BANKCOLLECT, map, reduce, clas);
	}

	@Override
	public <T> GroupByResults<T> group(Criteria criteria, String key, String map, String reduce, Class<T> clas) {
		return mongoTemplate.group(criteria, CollectionNames.BANKCOLLECT, GroupBy.key(key).initialDocument(map)
				.reduceFunction(reduce), clas);
	}

	@Override
	public <T> GroupByResults<T> groups(Criteria criteria, String map, String reduce, Class<T> clas, String... key) {
		return mongoTemplate.group(criteria, CollectionNames.BANKCOLLECT, GroupBy.key(key).initialDocument(map)
				.reduceFunction(reduce), clas);
	}

	/**
	 * pageNum 第几页 pageSize 每页多少条 orders 排序 criterias 条件
	 * 
	 * @return
	 */
	public Page<BankCollectDomain> getPageResult(Query query, BaseQueryOrder order) {
		return getPageResult(query, order, BankCollectDomain.class);
	}

	@Override
	public void update(Query query, Update update) {
		super.update(query, update, BankCollectDomain.class);
	}

	@Override
	public List<BankCollectDomain> findAll() {
		return mongoTemplate.findAll(BankCollectDomain.class);
	}
}
